#-*- encoding:UTF-8 -*-
import sys
import requests

from basic.data.data import getTime
from basic.utils.path import Path

htmlHead = "<!DOCTYPE html>" \
            "<html>" \
            "  <style>" \
            "  body {" \
            "      width: 800px;" \
            "      margin: 40px auto;" \
            "      font-family: 微软雅黑;" \
            "      font-size: 14px;" \
            "      color: #444;" \
            "  }" \
            "  table {" \
            "      *border-collapse: collapse; /* IE7 and lower */" \
            "      border-spacing: 0;" \
            "      width: 100%;" \
            "  }" \
            "  .bordered {" \
            "      border: solid #ccc 1px;" \
            "      -moz-border-radius: 6px;" \
            "      -webkit-border-radius: 6px;" \
            "      border-radius: 6px;" \
            "      -webkit-box-shadow: 0 1px 1px #ccc;" \
            "      -moz-box-shadow: 0 1px 1px #ccc;" \
            "      box-shadow: 0 1px 1px #ccc;" \
            "  }" \
            "  .bordered tr:hover {" \
            "      background: #fbf8e9;" \
            "      -o-transition: all 0.1s ease-in-out;" \
            "      -webkit-transition: all 0.1s ease-in-out;" \
            "      -moz-transition: all 0.1s ease-in-out;" \
            "      -ms-transition: all 0.1s ease-in-out;" \
            "      transition: all 0.1s ease-in-out;" \
            "  }" \
            "  .bordered td, .bordered th {" \
            "      border-left: 1px solid #ccc;" \
            "      border-top: 1px solid #ccc;" \
            "      padding: 10px;" \
            "      text-align: middle;" \
            "  }" \
            "  .errorRow {" \
            "    color:#FF4500;" \
            "    font-weight: bold;" \
            "  }"\
            "  .bordered th {" \
            "      background-color: #dce9f9;" \
            "      background-image: -webkit-gradient(linear, left top, left bottom, from(#ebf3fc), to(#dce9f9));" \
            "      background-image: -webkit-linear-gradient(top, #ebf3fc, #dce9f9);" \
            "      background-image:    -moz-linear-gradient(top, #ebf3fc, #dce9f9);" \
            "      background-image:     -ms-linear-gradient(top, #ebf3fc, #dce9f9);" \
            "      background-image:      -o-linear-gradient(top, #ebf3fc, #dce9f9);" \
            "      background-image:         linear-gradient(top, #ebf3fc, #dce9f9);" \
            "      -webkit-box-shadow: 0 1px 0 rgba(255,255,255,.8) inset;" \
            "      -moz-box-shadow:0 1px 0 rgba(255,255,255,.8) inset;" \
            "      box-shadow: 0 1px 0 rgba(255,255,255,.8) inset;" \
            "      border-top: none;" \
            "      text-shadow: 0 1px 0 rgba(255,255,255,.5);" \
            "  }" \
            "  .bordered td:first-child, .bordered th:first-child {" \
            "      border-left: none;" \
            "  }" \
            "  .bordered th:first-child {" \
            "      -moz-border-radius: 6px 0 0 0;" \
            "      -webkit-border-radius: 6px 0 0 0;" \
            "      border-radius: 6px 0 0 0;" \
            "  }" \
            "  .bordered th:last-child {" \
            "      -moz-border-radius: 0 6px 0 0;" \
            "      -webkit-border-radius: 0 6px 0 0;" \
            "      border-radius: 0 6px 0 0;" \
            "  }" \
            "  .bordered th:only-child{" \
            "      -moz-border-radius: 6px 6px 0 0;" \
            "      -webkit-border-radius: 6px 6px 0 0;" \
            "      border-radius: 6px 6px 0 0;" \
            "  }" \
            "  .bordered tr:last-child td:first-child {" \
            "      -moz-border-radius: 0 0 0 6px;" \
            "      -webkit-border-radius: 0 0 0 6px;" \
            "      border-radius: 0 0 0 6px;" \
            "  }" \
            "  .bordered tr:last-child td:last-child {" \
            "      -moz-border-radius: 0 0 6px 0;" \
            "      -webkit-border-radius: 0 0 6px 0;" \
            "      border-radius: 0 0 6px 0;" \
            "  }" \
            "  </style>" \
            "  <head>" \
            "    <meta http-equiv=\"content-type\" content=\"text/html:charset=utf-8\"></meta>" \
            "  </head>" \
            "  <body>"

htmlEnd = "</body></html>"


detailTableHead = "<h2>测试结果</h2>" \
            "    <table class=bordered>" \
            "      <tr>" \
            "        <th nowrap=\"nowrap\" width = \"25%\">用例数</th>" \
            "        <th nowrap=\"nowrap\" width = \"25%\">成功</th>" \
            "        <th nowrap=\"nowrap\" width = \"25%\">失败</th>" \
            "      </tr>"

__f_table_head = "<h2>失败用例详情</h2>" \
            "    <table class=bordered>" \
            "      <tr>" \
            "        <th nowrap=\"nowrap\" width = \"55%\">用例名称</th>" \
            "        <th nowrap=\"nowrap\" width = \"15%\">执行结果</th>" \
            "      </tr>"


def __get_html_head(title):
    return htmlHead+"<h1 align=\"middle\">"+title+"</h1>"


def td(value):
    value=str(value)
    if value!='成功':
        td = "<td class=errorRow>%s</td>" % value
    else:
        td = "<td>%s</td>" % value
    return td


def setDetailTable(total, success, fail):
    tr=''
    tr = tr.replace('</table>', '')+"<tr align='middle'>"+td(total)+td(success)+td(fail) +"</tr></table>"
    return tr


def __set_f_table_head(list):
    tr=''
    for i in list:
        if i[4] != '成功':
            tr =tr.replace('</table>', '')+"<tr align='middle'>"+td(i[2])+td("失败") +"</tr></table>"
    print(tr)
    return tr

def href(name):
    url="https://nnapptest.cloudbae.cn:37413/api/v1/file/"+name+".html"
    trendChart = "<h3 align=\"right\"><a href="+url+">点我查看全部</a></h3>"
    return trendChart


def get_report(result_list, total, success, fail, type):
    print(result_list)
    xml = __get_html_head('%s' % 'UI自动化测试报告') + detailTableHead + setDetailTable(total, success, fail) + __f_table_head + __set_f_table_head(result_list) + href(__get_file_url(type)) + htmlEnd
    print(xml)
    return  xml


def __get_file_url(type):
    name=str(getTime(2))
    url="https://nnapptest.cloudbae.cn:37413/api/v1/transferOssFile"
    file = {'file': open(Path.getProjectPath() + "/res/report/test.html", 'rb')}
    datas ={"fileKey": name+".html"}
    if type==0:requests.post(url, data=datas,files=file)
    return name
